<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Menus extends Admin_Controller{

    function Menus(){
        parent::Admin_Controller();
        $CI = & get_instance();
        $CI->load->module_define('menus');
        $CI->load->module_model('menus.menu');
    }
    
    function index(){
        $this->lists();
    }
    
    function lists(){
        $CI = & get_instance();
        $base_url = 'admin/menus.menus.lists';
        list($limit, $offset, $sort_field, $sort_type) = array($CI->config->item('rows_per_page'), 3, 4 ,5);
        
        $condition = '('.ADMIN_MENU_PARENT_ID.' = 0 OR '.ADMIN_MENU_PARENT_ID.' IS NULL OR '.ADMIN_MENU_PARENT_ID.' = '.ADMIN_MENU_ID.')';
        $this->_lists($condition, $base_url, $limit, $offset, $sort_field, $sort_type);
        $this->module_toolbar('menus.toolbar', array('parent_id' => 0));
        $this->module_navigation(get_class($this));
    }
    
    function child_lists(){
        $CI = & get_instance();
        $parent_id = $CI->uri->segment(3);
        $CI->menu->set_id($parent_id);
        if(!$CI->menu->get()){
            redirect('admin/menus.menus.lists');
        }

        $base_url = 'admin/menus.menus.child_lists';
        list($limit, $offset, $sort_field, $sort_type) = array($CI->config->item('rows_per_page'), 4, 5 ,6);
        $condition = array(ADMIN_MENU_PARENT_ID => $parent_id);
        $this->_lists($condition, $base_url.'/'.$parent_id, $limit, $offset, $sort_field, $sort_type);
        
        $parents = $CI->menu->parent_recursive();
        $s = '<a href="'.site_url($base_url).'">'.get_class($this).'</a>';
        foreach($parents as $menu_item){
            $s .= ' > <a href="'.site_url($base_url).'/'.$menu_item->id.'">'.h($menu_item->name).'</a>';
        }
        $s .= ' > '.$CI->menu->name;
        $this->module_navigation($s);
        
        $this->module_toolbar('menus.toolbar', array('parent_id' => $parent_id));
        $this->view('lists');
    }
    
    function _lists($condition, $base_url, $limit, &$offset=0, &$sort_field='name', &$sort_type='0'){
        $CI = & get_instance();
        $config['uri_segment'] = $offset;

        $offset = $CI->uri->segment($offset)?$CI->uri->segment($offset):0;
        $sort_field = $CI->uri->segment($sort_field)?$CI->uri->segment($sort_field):'name';
        $sort_type = $CI->uri->segment($sort_type)?'1':'0';

        $menus = $CI->menu->get_lists($condition, $limit, $offset, $sort_field, $sort_type);
        $this->add('menus', $menus);

        $CI->load->library('pagination');
        $config['base_url'] = site_url($base_url.'/%s/'.$sort_field.'/'.$sort_type);
        $config['total_rows'] = $CI->menu->get_numrows();
        $config['per_page'] = $limit;
        $CI->pagination->initialize($config);
        $this->add('paging', $CI->pagination->create_links());
        
        $this->add('NameTX', $this->sort_column('Name', 'name', $sort_type, $base_url.'/'.$offset, $sort_field));
        $this->add('OrderTX', $this->sort_column('Order', 'ordering', $sort_type, $base_url.'/'.$offset, $sort_field));
    }
    
    function add_new(){
        $CI = & get_instance();
        $CI->load->module_define('users');
        $CI->load->module_language('menus.menus');
        $CI->load->library('form_validation');
        $CI->load->helper('form');
        $CI->form_validation->set_rules('name', 'Name', 'trim|required');
        $CI->form_validation->set_rules('ordering', 'Order', 'trim|required|integer');

        if ($CI->form_validation->run() !== FALSE){
            $data = array(ADMIN_MENU_NAME => $CI->input->post('name'),
                          ADMIN_MENU_LINK => $CI->input->post('link'),
                          ADMIN_MENU_ORDERING => $CI->input->post('ordering'),
                          ADMIN_MENU_IMAGE => $CI->input->post('image_name'),
                          ADMIN_MENU_PARENT_ID => $CI->input->post('parent_id'),
                          ADMIN_MENU_ACCESS_LEVEL => $CI->input->post('access_level'),
                          );
            if($CI->menu->save($data)){
                $CI->menu->clear_cache();
                set_flash_message(lang('menu_add_success'));
                if($CI->uri->segment(3))
                    redirect('admin/menus.menus.child_lists/'.$CI->uri->segment(3));
                else
                    redirect('admin/menus.menus.lists/');
            }
        }

        $menus = $CI->menu->menu_list_tree();
        $menu_tree = $CI->parser->module_parse('menus.menu_tree', array('menus' => $menus,'parent_id' => ($CI->input->post('parent_id')!='')?$CI->input->post('parent_id'):$CI->uri->segment(3)), TRUE);
        $this->add('menu_tree', $menu_tree);

        $CI->load->module_helper('menus.menu_images');
        $this->add('images', menu_images_list());

        $this->add('parent_id', $CI->uri->segment(3));
        $this->add('image_name', $CI->input->post('image_name'));
        $this->add('name', $CI->input->post('name'));
        $this->add('link', $CI->input->post('link'));
        $this->add('ordering', $CI->input->post('ordering'));
        $this->add('access_level', $CI->input->post('access_level'));
        
        $this->module_navigation(get_class($this).' : '.lang('menu_add_title'));
        $this->view('add_new');
    }
    
    function edit(){
        $CI = & get_instance();
        $CI->load->module_language('menus.menus');
        $menu_id = $CI->uri->segment(3);
        $CI->menu->set_id($menu_id);
        if(!$CI->menu->get()){
            set_flash_message(lang('menu_edit_not_found'));
            redirect('admin/menus.menus.lists/');
        }
        
        $CI->load->module_define('users');
        $CI->load->library('form_validation');
        $CI->load->helper('form');
        $CI->form_validation->set_rules('name', 'Name', 'trim|required');
        $CI->form_validation->set_rules('ordering', 'Order', 'trim|required|integer');

        if ($CI->form_validation->run() !== FALSE){
            $data = array(ADMIN_MENU_NAME => $CI->input->post('name'),
                          ADMIN_MENU_LINK => $CI->input->post('link'),
                          ADMIN_MENU_ORDERING => $CI->input->post('ordering'),
                          ADMIN_MENU_IMAGE => $CI->input->post('image_name'),
                          ADMIN_MENU_PARENT_ID => $CI->input->post('parent_id'),
                          ADMIN_MENU_ACCESS_LEVEL => $CI->input->post('access_level'),
                          );
            if($CI->menu->save($data)){
                $CI->menu->clear_cache();
                set_flash_message(lang('menu_edit_success'));
                if($CI->input->post('parent_id'))
                    redirect('admin/menus.menus.child_lists/'.$CI->input->post('parent_id'));
                else
                    redirect('admin/menus.menus.lists/');
            }
        }

        $menus = $CI->menu->menu_list_tree();
        $menu_tree = $CI->parser->module_parse('menus.menu_tree', array('menus' => $menus,'parent_id' => ($CI->input->post('parent_id')!='')?$CI->input->post('parent_id'):$CI->menu->parent_id), TRUE);
        $this->add('menu_tree', $menu_tree);

        $CI->load->module_helper('menus.menu_images');
        $this->add('images', menu_images_list());

        $this->add('parent_id', ($CI->input->post('parent_id')!='')?$CI->input->post('parent_id'):$CI->menu->parent_id);
        $this->add('image_name', ($CI->input->post('image_name'))?$CI->input->post('image_name'):$CI->menu->admin_menu_image);
        $this->add('name', ($CI->input->post('name'))?$CI->input->post('name'):$CI->menu->name);
        $this->add('link', ($CI->input->post('link'))?$CI->input->post('link'):$CI->menu->admin_menu_link);
        $this->add('ordering', ($CI->input->post('ordering'))?$CI->input->post('ordering'):$CI->menu->ordering);
        $this->add('access_level', ($CI->input->post('access_level'))?$CI->input->post('access_level'):$CI->menu->access_level);
        
        $this->module_navigation(get_class($this).' : '.lang('menu_edit_title'));
        $this->view('add_new');
    }
    
    function delete(){
        $CI = & get_instance();
        $CI->menu->set_id($CI->uri->segment(3));
        $CI->menu->delete();
        $CI->menu->clear_cache();
        set_flash_message(lang('menu_delete_success'));
        redirect('admin/menus.menus.lists/');
    }
}